package com.fanfanwang.ffw.app.ui.service;

import java.util.ArrayList;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.adapter.ServiceCarWashListAdapter;
import com.fanfanwang.ffw.app.asynctask.GetGasStationDianListAsyncTask;
import com.fanfanwang.ffw.app.base.BaseActivity;
import com.fanfanwang.ffw.app.bean.DianListBean;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

/**
 * 加油站列表
 * 
 * @author Administrator
 * 
 */
public class GasStationListAct extends BaseActivity {
	private GasStationListAct mAct;
	private CommonTitleBar commonTitleBar;
	public static PullToRefreshListView mPullToRefreshListView;
	private ListView listView;
	private ServiceCarWashListAdapter adapter;
	public int page = 1;
	public static IUpdateServiceCarWash iUpdateServiceCarWash;
	private ArrayList<DianListBean> lists;
	private EditText et_find;
	private LocationClient locClient;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
		AppManager.getAppManager().addActivity(this);
		 getLocation();
		initData();
		setListener();
	}
	/**
	 * 定位获取当前经纬度
	 */
	public void  getLocation(){
		//实例化位置客户端19        
		locClient = new LocationClient(getApplicationContext());
		//创建一个位置option对象21   
	    LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);// 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(5000);
        option.setIsNeedAddress(true); //返回的信息包含当前的地址26   
		//将封装的参数设置到位置客户端32        
		locClient.setLocOption(option);
		locClient.start();
		locClient.registerLocationListener(new BDLocationListener() {
			
			@Override
			public void onReceiveLocation(BDLocation loc) {
				// TODO Auto-generated method stub
				AppConstant.NOWLATITUDE = loc.getLatitude();
				AppConstant.NOWLONGITUDE = loc.getLongitude();
				AppConstant.NOWADDRESS=loc.getAddrStr();
			}
		});
	}
	public void setCallback() {
		iUpdateServiceCarWash = new IUpdateServiceCarWash() {

			@Override
			public void setServiceCarWashList(ArrayList<DianListBean> beans) {
				// TODO Auto-generated method stub
				lists.clear();
				lists.addAll(beans);
				adapter.resetData(beans);
				adapter.notifyDataSetChanged();
			}
		};
	}

	@Override
	public void initData() {
		// // TODO Auto-generated method stub
		commonTitleBar.setCenterText(getString(R.string.gas_title));
		commonTitleBar.setReturnVisible();
		setCallback();
		listView.setAdapter(adapter);
		// 2代表加油站的意思
		new GetGasStationDianListAsyncTask(mAct, "2", AppConstant.NOWLONGITUDE
				+ "", AppConstant.NOWLATITUDE + "").execute("");

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// MyObserver.unregiserOberverBitmap();
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_service_carwash);
		mAct = this;
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		// mPullToRefreshListView = (PullToRefreshListView)
		// findViewById(R.id.lv_carwash);
		// mPullToRefreshListView.setMode(Mode.PULL_FROM_END);
		// listView = mPullToRefreshListView.getRefreshableView();
		listView = (ListView) findViewById(R.id.lv_carwash);
		lists = new ArrayList<DianListBean>();
		adapter = new ServiceCarWashListAdapter(mAct, lists);
		et_find = (EditText) findViewById(R.id.et_find);
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		commonTitleBar.setToolBarReturnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		et_find.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				String temp = s.toString();
				ArrayList<DianListBean> tempLists = new ArrayList<DianListBean>();
				if (!TextUtils.isEmpty(temp)) {
					for (int i = 0; i < lists.size(); i++) {
						// if((serviceCarlists.get(i).distance.contains(temp))||(serviceCarlists.get(i).title.contains(temp))){
						if (AppConstant.URLDecoderString(lists.get(i).juli)
								.contains(temp)) {
							tempLists.add(lists.get(i));
						} else if (AppConstant.URLDecoderString(
								lists.get(i).name).contains(temp)) {
							tempLists.add(lists.get(i));
						}
					}
					// TODO
					// 重新生成adapter给listview赋值成功了。但是直接改变adapter的值以前的serviceCarlists的值就全部消失了？
					// adapter=new ServiceCarWashListAdapter(mAct, tempLists);
					// listView.setAdapter(adapter);
					adapter.resetData(tempLists);
					adapter.notifyDataSetChanged();

				} else {
					adapter.resetData(lists);
					adapter.notifyDataSetChanged();
					// adapter=new ServiceCarWashListAdapter(mAct, tempLists);
					// listView.setAdapter(adapter);
				}

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

	}

	public interface IUpdateServiceCarWash {
		public void setServiceCarWashList(ArrayList<DianListBean> lists);
	}
}
